package com.es.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 描述:
 * es-servlet-
 *
 * @author Derek
 * 创建时间：2022-04-21 11:26
 */
@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //
        resp.setContentType("text/html;charset=utf-8");
        //
        Cookie cookieUsername = new Cookie("username",username);
        Cookie cookiePassword = new Cookie("password",password);
        //设置cookie的最大存活时间  登录
        cookiePassword.setMaxAge(60*60);
        cookieUsername.setMaxAge(60*60);
        //将cookie写到客户端
        resp.addCookie(cookiePassword);
        resp.addCookie(cookieUsername);


        PrintWriter out = resp.getWriter();
        out.println("您的用户名是："+username);
        out.println("<br>你的密码是："+password);

        out.flush();

    }
}
